package com.gdpu.project.action.achievementManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import com.gdpu.page.vo.PageView;
import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.service.ChengGuoService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.gdpu.project.util.DateUtil;
import com.gdpu.project.vo.CgConference;
import com.gdpu.project.vo.CgInnovate;
import com.gdpu.project.vo.CgPatent;
import com.gdpu.project.vo.CgProject;
import com.gdpu.project.vo.CgThesis;
import com.gdpu.project.vo.CgTreatise;
import com.gdpu.project.vo.Teacher;

public class AchievementSearchAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String college;
	private String department;
	private String t_position;
	private String education;
	private String teacherId;
	private String achievementType;
	private String startTime;
	private String endTime;
	
	/**
	 * 当前页
	 */
	private int currentpage=1;
	
	
	//private HttpServletRequest request;
	
	private ChengGuoService chengguoService;
	private String cataloge;
	
	public String getCollege() {
		return college;
	}

	public void setCollege(String college) {
		this.college = college;
	}

	
	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	

	

	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public String getAchievementType() {
		return achievementType;
	}

	public void setAchievementType(String achievementType) {
		this.achievementType = achievementType;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void setCataloge(String cataloge) {
		this.cataloge = cataloge;
	}

	public String getCataloge() {
		return cataloge;
	}

	public void setChengguoService(ChengGuoService chengguoService) {
		this.chengguoService = chengguoService;
	}
	
	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}
	
	public int getCurrentpage() {
		return currentpage;
	}

	public String getT_position() {
		return t_position;
	}

	public void setT_position(String t_position) {
		this.t_position = t_position;
	}


	// 高級查詢
	public String advanceSearch() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		
		if ("kyxm".equals(achievementType)) {
			PageView<CgProject>pageView=new PageView<CgProject>(10,currentpage);//默认每页10条记录
			QueryResult<CgProject> queryResult = chengguoService
			.searchCgProjectByDate(college, department,t_position, education,teacherId,cataloge, DateUtil
					.stringToDate(startTime), DateUtil
					.stringToDate(endTime),pageView.getFirstResult(),pageView.getMaxresult());
			pageView.setQueryResult(queryResult);
			request.setAttribute("pageView", pageView);
			ctx.put("cgProjectlist", queryResult.getResultlist());
			return "kyxm";
		} else if ("lw".equals(achievementType)) {
			PageView<CgThesis>pageView=new PageView<CgThesis>(10,currentpage);//默认每页10条记录
			QueryResult<CgThesis> queryResult = chengguoService
					.searchCgThesisByDate(college, department,t_position, education,teacherId,cataloge, DateUtil
							.stringToDate(startTime), DateUtil
							.stringToDate(endTime),pageView.getFirstResult(),pageView.getMaxresult());
			pageView.setQueryResult(queryResult);
			request.setAttribute("pageView", pageView);
			ctx.put("cgThesislist", queryResult.getResultlist());
			return "lw";

		} else if ("xshy".equals(achievementType)) {
			PageView<CgConference>pageView=new PageView<CgConference>(10,currentpage);//默认每页10条记录
			QueryResult<CgConference> queryResult = chengguoService
					.searchCgConferenceByDate(college, department,t_position, education,teacherId,cataloge, DateUtil
							.stringToDate(startTime), DateUtil
							.stringToDate(endTime),pageView.getFirstResult(),pageView.getMaxresult());
			pageView.setQueryResult(queryResult);
			request.setAttribute("pageView", pageView);
			ctx.put("cgConferencelist", queryResult.getResultlist());
			return "xshy";

		} else if ("zdxskjcx".equals(achievementType)) {
			PageView<CgInnovate>pageView=new PageView<CgInnovate>(10,currentpage);//默认每页10条记录
			QueryResult<CgInnovate> queryResult = chengguoService
					.searchCgInnovateByDate(college, department,t_position, education,teacherId,cataloge, DateUtil
							.stringToDate(startTime), DateUtil
							.stringToDate(endTime),pageView.getFirstResult(),pageView.getMaxresult());
			pageView.setQueryResult(queryResult);
			request.setAttribute("pageView", pageView);
			ctx.put("cgInnovatelist", queryResult.getResultlist());
			return "zdxskjcx";

		} else if ("zl".equals(achievementType)) {
			PageView<CgPatent>pageView=new PageView<CgPatent>(10,currentpage);//默认每页10条记录
			QueryResult<CgPatent> queryResult = chengguoService
					.searchCgPatentByDate(college, department,t_position, education,teacherId,cataloge, DateUtil
							.stringToDate(startTime), DateUtil
							.stringToDate(endTime),pageView.getFirstResult(),pageView.getMaxresult());
			pageView.setQueryResult(queryResult);
			request.setAttribute("pageView", pageView);
			
			ctx.put("cgPatentlist", queryResult.getResultlist());
			return "zl";

		} else {
			PageView<CgTreatise>pageView=new PageView<CgTreatise>(10,currentpage);//默认每页10条记录
			QueryResult<CgTreatise> queryResult = chengguoService
			.searchCgTreatiseByDate(college, department,t_position, education,teacherId,cataloge, DateUtil
					.stringToDate(startTime), DateUtil
					.stringToDate(endTime),pageView.getFirstResult(),pageView.getMaxresult());
			pageView.setQueryResult(queryResult);
			request.setAttribute("pageView", pageView);
			ctx.put("cgTreatiselist", queryResult.getResultlist());
			return "zz";

		}
	}
/**
 * 将一个Map类型成果转化成具体的某个成果的List类型，比如：从Map<Teacher, List<CgPatent>>到List&ltList&ltCgPatent&gt&gt
 * @param <T>
 * @param results
 * @param cgType
 * @return
 */
	
	private <T> List<List<T>> map2List(Map<Teacher, List<T>> results,Class<T> cgType) {
		List<List<T>> specificCgList = new ArrayList<List<T>>();
		for (Teacher teacher : results.keySet()) {
			specificCgList.add(results.get(teacher));
		}
		return specificCgList;
	}

	

	
}

